package cn.lg.soar.system.general.service.impl;

import cn.lg.soar.common.model.OneToManyDTO;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.system.auth.entity.User;
import cn.lg.soar.system.general.entity.UserTag;
import cn.lg.soar.system.general.entity.UserTagItem;
import cn.lg.soar.system.general.mapper.UserTagItemMapper;
import cn.lg.soar.system.general.mapper.UserTagMapper;
import cn.lg.soar.system.auth.service.IUserService;
import cn.lg.soar.system.general.service.IUserTagService;
import cn.lg.soar.system.general.service.UserTagItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * UserTag服务层实现
 * 租户
 * @author luguoxiang
 * @since 2022-04-30
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class UserTagServiceImpl extends ServiceImpl<UserTagMapper, UserTag> implements IUserTagService {

    @Resource
    private IUserService userService;
    @Resource
    private UserTagItemService userTagItemService;

    @Override
    public boolean removeByIds(Collection<?> ids) {
        if (DataUtil.isEmpty(ids)) {
            return true;
        }
        userTagItemService.remove(
                Wrappers.<UserTagItem>lambdaQuery()
                        .in(UserTagItem::getTagId, ids)
        );
        AssertUtil.isTrue(super.removeByIds(ids), "操作成功");
        return true;
    }

    @Override
    public List<User> users(Long tagId) {
        return userService.list(
                Wrappers.<User>lambdaQuery()
                        .select(User::getId, User::getUsername, User::getNickname, User::getAvatar)
                        .exists("select * from lg_sys_user_2_tag r where lg_sys_user.id=r.user_id and r.tag_id =" + tagId)
        );
    }

    @Override
    public List<UserTag> tags(Long userId) {
        return list(
                Wrappers.<UserTag>lambdaQuery()
                        .select(UserTag::getId, UserTag::getName)
                        .eq(UserTag::getDisabled, false)
                        .eq(UserTag::getUserId, UserContext.getUserId())
                        .exists("select * from lg_sys_user_2_tag r where lg_sys_user_tag.id=r.tag_id and r.user_id =" + userId)
        );
    }

    @Override
    public void addUser(OneToManyDTO<Long, Long> dto) {
        Long tagId = dto.getMainId();
        List<UserTagItem> items = dto.getJoinIds().stream().map(x -> {
            UserTagItem userTagItem = new UserTagItem();
            userTagItem.setTagId(tagId);
            userTagItem.setUserId(x);
            return userTagItem;
        }).collect(Collectors.toList());
        AssertUtil.isTrue(userTagItemService.saveBatch(items), "操作成功");
    }

    @Override
    public void removeUser(List<Long> ids) {
        AssertUtil.isTrue(userTagItemService.removeBatchByIds(ids), "操作成功");
    }

    @Override
    public PageResult<User> queryUser(Long tagId, PageQuery query, User user) {
        Page<User> page = userTagItemService.queryUser(new Page<>(query.getPageNo(), query.getPageSize()), tagId, user);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
